package com.tender.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * URL 编码与解码工具类
 */
@Slf4j
public class UrlEncodeAndDecodeUtil {

    public static String encode(String str) {
        Assert.hasLength(str, "待编码的字符串不能为空!");
        try {
            return URLEncoder.encode(str, "UTF-8");
        } catch (Exception e) {
            log.error("字符串 str = {},编码异常:", str, e);
        }

        return null;
    }

    public static String decode(String str) {
        Assert.hasLength(str, "待解码的字符串不能为空!");
        try {
            return URLDecoder.decode(str, "UTF-8");
        } catch (Exception e) {
            log.error("字符串 str = {},解码异常:", str, e);
        }

        return null;
    }

    public static void main(String[] args) {
        String hello = "hello@";
        String encode = encode(hello);
        System.out.println(encode);
        String decode = decode(encode);
        System.out.println(decode);
    }

}
